package cn.iocoder.yudao.module.yyzx.controller.admin.service_object;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotations.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.yyzx.controller.admin.service_object.vo.*;
import cn.iocoder.yudao.module.yyzx.dal.dataobject.service_object.Service_objectDO;
import cn.iocoder.yudao.module.yyzx.service.service_object.Service_objectService;

@Tag(name = "管理后台 - 服务对象")
@RestController
@RequestMapping("/yyzx/service_object")
@Validated
public class Service_objectController {

    @Resource
    private Service_objectService service_objectService;

    @PostMapping("/create")
    @Operation(summary = "创建服务对象")
    @PreAuthorize("@ss.hasPermission('yyzx:service_object:create')")
    public CommonResult<Integer> createService_object(@Valid @RequestBody Service_objectSaveReqVO createReqVO) {
        return success(service_objectService.createService_object(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新服务对象")
    @PreAuthorize("@ss.hasPermission('yyzx:service_object:update')")
    public CommonResult<Boolean> updateService_object(@Valid @RequestBody Service_objectSaveReqVO updateReqVO) {
        service_objectService.updateService_object(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除服务对象")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('yyzx:service_object:delete')")
    public CommonResult<Boolean> deleteService_object(@RequestParam("id") Integer id) {
        service_objectService.deleteService_object(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得服务对象")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('yyzx:service_object:query')")
    public CommonResult<Service_objectRespVO> getService_object(@RequestParam("id") Integer id) {
        Service_objectDO service_object = service_objectService.getService_object(id);
        return success(BeanUtils.toBean(service_object, Service_objectRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得服务对象分页")
    @PreAuthorize("@ss.hasPermission('yyzx:service_object:query')")
    public CommonResult<PageResult<Service_objectRespVO>> getService_objectPage(@Valid Service_objectPageReqVO pageReqVO) {
        PageResult<Service_objectDO> pageResult = service_objectService.getService_objectPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, Service_objectRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出服务对象 Excel")
    @PreAuthorize("@ss.hasPermission('yyzx:service_object:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportService_objectExcel(@Valid Service_objectPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<Service_objectDO> list = service_objectService.getService_objectPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "服务对象.xls", "数据", Service_objectRespVO.class,
                        BeanUtils.toBean(list, Service_objectRespVO.class));
    }

}